import time
import json
import csv

'''
将冷库数据写入到excel
'''


def main():
    print("开始爬取冷库数据" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    f = open("E:\cold_storage\coldData10.txt", encoding="utf-8")
    data = f.read()
    # print(data)
    jsonData = json.loads(data, encoding='utf-8')
    dataArray = jsonData['Tag']
    # print(dataArray)

    #  1.创建文件对象
    csvf = open('E:\cold_storage\csv_file.csv', 'w', encoding='utf-8')
    #
    # #  2.基于文件对象构建csv写入对象
    csv_write = csv.writer(csvf)
    #
    # #  3.构建列表头
    title = ['id', 'enterpriceId', 'name', 'code', 'address', 'areacode', 'longitude', 'latitude', 'volume', 'activevolume',
     'areaSize', 'usableAreaSize', 'rentalLand', 'rentalLandUnit', 'rentalLandDay', 'rentalLandDayUnit', 'rentalZero',
     'rentalZeroUnit', 'score', 'limitproducts', 'introduce', 'simpleInfo', 'content', 'remark', 'linkman',
     'linkmanTel', 'tel', 'floor', 'height', 'refrigerantType', 'haveCargoArea', 'haveClosePlatform',
     'havaTemperatureRecord', 'applytime', 'establishTime', 'passtime', 'status', 'createTime', 'createUserid',
     'createUsername', 'image', 'isDelete', 'provinceCode', 'cityCode', 'areaSizeUnit', 'volumeUnit', 'policy',
     'isSpjgc', 'isSjjba', 'isDblk', 'dblkUrl', 'establishYear', 'ceditCode', 'isShelf', 'countryName', 'provinceName',
     'cityName', 'regionName', 'scoreName', 'rice', 'underTitle', 'areaName', 'businessTypeName', 'warehouseAreaName',
     'warehouseTypeName', 'warehouseTypeNameList', 'storeTypeName', 'activevolumeName', 'warehouseDynamic',
     'warehouseTypeRelationDtoList', 'certifUrls', 'swiper', 'facilityPic', 'storeFlag', "租仓类型", "经营类型", "存储类型", "仓库类型"]

    # csv_write.writerow(['学号', '班级', '姓名'])
    csv_write.writerow(title)
    #
    # #  4.写入csv文件
    # csv_write.writerow(['001', '2021.1', '张三'])
    # csv_write.writerow(['002', '2021.1', '李四'])
    # csv_write.writerow(['003', '2021.1', '王五'])
    # csv_write.writerow(['004', '2021.1', '老六'])


    for item in dataArray:
        # print(item)
        targetArr = []
        for key, value in item.items():

            v = ""
            if value is not None:
                v = str(value)

            typeName = "-"
            relationType = ''
            if key == 'warehouseTypeRelationDtoList':
                warehouseTypeRelationDtoList = eval(v)
                for dto in warehouseTypeRelationDtoList:
                    for k1, v1 in dto.items():
                        if k1 == 'baseType':
                            typeName = v1
                            # relationType += v1 + "|"
                            # print(typeName)
                        else:
                            for item1 in v1:
                                # warehouseTypeRelationList
                                for k2, v2 in item1.items():
                                    if k2 == "type":
                                        relationType += v2 + "|"

                        targetArr.append(relationType)
                    # print(relationType)

                    # relationType += "\n\r"
            else:
                targetArr.append(value)

        print(targetArr)
        csv_write.writerow(targetArr)

    #  5.关闭文件
    csvf.close()




    print("冷库数据爬取完成" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))


if __name__ == '__main__':
    main()
